﻿using System;
using System.Text;

namespace Mars.GameCore
{
    /// <summary>
    /// Json序列化帮助类
    /// </summary>
    public static class JsonSerializerHelper
    {
        /// <summary>
        /// 序列化对象
        /// </summary>
        /// <param name="value"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static byte[] Serialize<T>(T value)
        {
            var json = JsonUtility.ToJson(value);
            return Encoding.UTF8.GetBytes(json);
        }

        /// <summary>
        /// 反序列化数据对象
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="type">类型</param>
        /// <returns></returns>
        public static object Deserialize(byte[] data, Type type)
        {
            var json = Encoding.UTF8.GetString(data);
            return JsonUtility.ToObject(type, json);
        }
    }
}